# -*- coding: UTF-8 -*-


from .. import http


class Webclient(http.Controller):
    def version_info(self):
        url = '/web/webclient/version_info'
        return self(url)

    def version(self):
        url = '/jsonrpc'
        kws = {
            'service': 'common',
            'method': 'version',
            'args': []
        }
        return self(url, **kws)


class Database(http.Controller):
    def list(self):
        url = '/web/database/list'
        return self(url)


class Dataset(http.Controller):
    def call_kw(self, model, method, args, kwargs):
        url = '/web/dataset/call_kw'
        url = url + '/' + model + '/' + method
        return self(url, model=model, method=method, args=args, kwargs=kwargs)


class Action(http.Controller):
    def load(self, action_id, additional_context=None):
        url = '/web/action/load'
        return self(url, action_id=action_id, additional_context=additional_context)


class Session(http.Controller):
    @property
    def session_info(self):
        return self._session_info or {}

    @property
    def user_context(self):
        info = self.session_info
        context = info['user_context'].copy()
        context['allowed_company_ids'] = self.allowed_company_ids
        return context

    @property
    def context(self):
        return self.user_context

    @property
    def current_company_id(self):
        user_companies = self.session_info['user_companies']
        current_company = user_companies['current_company']
        return current_company

    @property
    def allowed_company_ids(self):
        return [self.current_company_id]

    def authenticate(self, **kwargs):
        url = '/web/session/authenticate'
        session_info = self(url, **kwargs)
        self._session_info = session_info
        return session_info

    def get_session_info(self):
        url = '/web/session/get_session_info'
        session_info = self(url)
        self._session_info = session_info
        return session_info

    def check(self):
        url = '/web/session/check'
        return self(url)

    def destroy(self):
        url = '/web/session/destroy'
        res = self(url)
        self._session_info = None
        return res

#     @property
#     def server_version(self):
#         info = self.session_info
#         server_version_info = info.get('server_version_info', [15, 0])
#         return server_version_info[0]

#     @property
#     def current_company_id(self):
#         info = self.session_info
#         user_companies = info.get('user_companies', {})
#         current_company = user_companies.get('current_company')
#         return current_company

#     _allowed_company_ids = []

#     @property
#     def allowed_company_ids(self):
#         cids = self._allowed_company_ids
#         if not cids:
#             return [self.current_company_id]

#         info = self.session_info
#         user_companies = info.get('user_companies', {})
#         allowed_companies = user_companies.get('allowed_companies', {})

#         odoo_cids = [comp['id'] for comp in allowed_companies.values()]

#         to_remove_cids = [cid for cid in cids if cid not in odoo_cids]

#         if not to_remove_cids:
#             return cids

#         todo = [cid for cid in cids if cid in odoo_cids]

#         if todo:
#             self._allowed_company_ids = todo
#             return todo

#         todo2 = odoo_cids[0:1]
#         self._allowed_company_ids = todo2
#         return todo2

#     @allowed_company_ids.setter
#     def allowed_company_ids(self, cids):
#         if not cids:
#             cids = [self.current_company_id]
#         self._allowed_company_ids = cids

#     @property
#     def user_context(self):
#         info = self.session_info
#         context = info.get('user_context', {})
#         allowed_company_ids = self.allowed_company_ids

#         user_context = {}
#         user_context.update(context)
#         user_context.update({'allowed_company_ids': allowed_company_ids})
#         return user_context

#     @property
#     def context(self):
#         return self.user_context


class Home(object):
    def __init__(self, base_url, timeout):
        self.webclient = Webclient(base_url, timeout)
        self.database = Database(base_url, timeout)
        self.session = Session(base_url, timeout)
        self.dataset = Dataset(base_url, timeout)
        self.action = Action(base_url, timeout)


WEB = Home
